<template>
    <IModal v-model="defaultValue" :loading="loading" :width="450">
        <Form :model="data" :rules="ruleValidate" ref="formCreate" label-position="top">
            <FormItem label="下载模板">
                <Input v-model="data.hs_code">
                    <Button slot="append" icon="md-arrow-round-down" @click="downloadTemplate">下载</Button>
                </Input>
            </FormItem>
            <FormItem label="模板类型" prop="type">
                <Select v-model="data.type">
                    <Option :value="0">产品模板</Option>
                    <Option :value="1">申报要素</Option>
                </Select>
            </FormItem>
            <FormItem label="模板文件" prop="file">
                <UploadFile v-model="data.file" url="common/index/upload"></UploadFile>
            </FormItem>
            <FormItem label="备注" prop="remark">
                <Input v-model="data.remark" type="textarea" :rows="4"></Input>
            </FormItem>
        </Form>
        <div slot="footer">
            <Button type="primary" icon="ios-add" @click="submit('formCreate')">提交</Button>
            <Button type="warning" icon="md-log-out" @click="defaultValue = false">返回</Button>
        </div>
    </IModal>
</template>

<script>
    import IModal from "../../../../components/layout/IModal";
    import IDrawerMixins from "../../../../mixins/iDrawerMixins";
    import UploadImage from "../../../../components/form/uploadImage";
    import UploadFile from "../../../../components/form/uploadFile";

    export default {
        name: "export-element",
        components: {UploadFile, UploadImage, IModal},
        mixins: [IDrawerMixins],
        data() {
            return {
                data: {},
                ruleValidate: {
                    file: [{required: true}],
                    type: [{required: true}]
                },
            }
        },
        methods: {
            submit(name) {
                this.validate(name).then(() => {
                    this.loading = true;
                    this.$http.post(`product/import`, this.data).then(() => {
                        this.defaultValue = false;
                    }).finally(() => {
                        this.loading = false;
                    });
                });
            },
            downloadTemplate(){
                if(Boolean(this.data.hs_code)){
                    window.open(`${this.$http.defaults.baseURL}common/download/hs_code?code=${this.data.hs_code}&_token=${'bearer ' + this.$store.state.common.token}`)
                }
            }
        }
    }
</script>

<style scoped>
    .export-box {
        max-height: 500px;
        overflow-y: scroll;
        padding: 15px;
    }
</style>
